﻿using System;
using RpcClient;
using RpcHelper;
using Wedonek.RpcStore.Gateway.Interface;
using Wedonek.RpcStore.Gateway.Model;
using Wedonek.RpcStore.Service.Interface;
using Wedonek.RpcStore.Service.Model;

namespace Wedonek.RpcStore.Gateway.Service
{
        internal class VerRouteService : IVerRouteService
        {
                private readonly IVerRouteCollect _VerRoute;
                private readonly IServerTypeCollect _ServerType;
                public VerRouteService (IVerRouteCollect route, IServerTypeCollect serverType)
                {
                        this._ServerType = serverType;
                        this._VerRoute = route;
                }

                public Guid Add (VerRouteDatum add)
                {
                        ServiceVerRoute route = add.ConvertMap<VerRouteDatum, ServiceVerRoute> ();
                        return this._VerRoute.Add (route);
                }

                public void Delete (Guid id)
                {
                        ServiceVerRoute route = this._VerRoute.Get (id);
                        this._VerRoute.Delete (route);
                }

                public ServiceVerRoute Get (Guid id)
                {
                        return this._VerRoute.Get (id);
                }

                public VerRoute[] GetRoutes (long verId)
                {
                        ServiceVerRoute[] routes = this._VerRoute.GetRoutes (verId);
                        if (routes.Length == 0)
                        {
                                return null;
                        }
                        ServerType[] types = this._ServerType.Gets (routes.Distinct (a => a.SystemTypeId));
                        return routes.ConvertMap<ServiceVerRoute, VerRoute> ((a, b) =>
                        {
                                b.BeginVer = a.BeginVer.FormatVerNum ();
                                b.EndVer = a.EndVer.FormatVerNum ();
                                b.SystemType = types.Find (c => c.Id == a.SystemTypeId, c => c.SystemName);
                                return b;
                        });
                }

                public void Set (Guid id, VerRouteSet set)
                {
                        ServiceVerRoute route = this._VerRoute.Get (id);
                        this._VerRoute.Set (route, set);
                }

                public void SetIsEnable (Guid id, bool isEnable)
                {
                        ServiceVerRoute route = this._VerRoute.Get (id);
                        this._VerRoute.SetIsEnable (route, isEnable);
                }
        }
}
